﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Terraria.Localization;
using Terraria.Map;

namespace Terraria.ModLoader
{
	// Token: 0x020001BB RID: 443
	internal static class MapLoader
	{
		// Token: 0x060011D9 RID: 4569 RVA: 0x0000F019 File Offset: 0x0000D219
		internal static int modTileOptions(ushort type)
		{
			return MapLoader.tileEntries[type].Count;
		}

		// Token: 0x060011DA RID: 4570 RVA: 0x0000F02B File Offset: 0x0000D22B
		internal static int modWallOptions(ushort type)
		{
			return MapLoader.wallEntries[type].Count;
		}

		// Token: 0x060011DB RID: 4571 RVA: 0x0040B9B8 File Offset: 0x00409BB8
		internal static void SetupModMap()
		{
			if (Main.dedServ)
			{
				return;
			}
			Array.Resize<ushort>(ref MapHelper.tileLookup, TileLoader.TileCount);
			Array.Resize<ushort>(ref MapHelper.wallLookup, WallLoader.WallCount);
			IList<Color> list = new List<Color>();
			IList<LocalizedText> list2 = new List<LocalizedText>();
			foreach (ushort num in MapLoader.tileEntries.Keys)
			{
				MapHelper.tileLookup[(int)num] = (ushort)((int)MapHelper.modPosition + list.Count);
				foreach (MapEntry mapEntry in MapLoader.tileEntries[num])
				{
					ushort key = (ushort)((int)MapHelper.modPosition + list.Count);
					MapLoader.entryToTile[key] = num;
					MapLoader.nameFuncs[key] = mapEntry.getName;
					list.Add(mapEntry.color);
					if (mapEntry.name != null)
					{
						list2.Add(mapEntry.name);
					}
					else
					{
						list2.Add(Language.GetText(mapEntry.translation.Key));
					}
				}
			}
			foreach (ushort num2 in MapLoader.wallEntries.Keys)
			{
				MapHelper.wallLookup[(int)num2] = (ushort)((int)MapHelper.modPosition + list.Count);
				foreach (MapEntry mapEntry2 in MapLoader.wallEntries[num2])
				{
					ushort key2 = (ushort)((int)MapHelper.modPosition + list.Count);
					MapLoader.entryToWall[key2] = num2;
					MapLoader.nameFuncs[key2] = mapEntry2.getName;
					list.Add(mapEntry2.color);
					if (mapEntry2.name != null)
					{
						list2.Add(mapEntry2.name);
					}
					else
					{
						list2.Add(Language.GetText(mapEntry2.translation.Key));
					}
				}
			}
			Array.Resize<Color>(ref MapHelper.colorLookup, (int)MapHelper.modPosition + list.Count);
			Lang._mapLegendCache.Resize((int)MapHelper.modPosition + list2.Count);
			for (int i = 0; i < list.Count; i++)
			{
				MapHelper.colorLookup[(int)MapHelper.modPosition + i] = list[i];
				Lang._mapLegendCache[(int)MapHelper.modPosition + i] = list2[i];
			}
			MapLoader.initialized = true;
		}

		// Token: 0x060011DC RID: 4572 RVA: 0x0040BC7C File Offset: 0x00409E7C
		internal static void UnloadModMap()
		{
			MapLoader.tileEntries.Clear();
			MapLoader.wallEntries.Clear();
			if (Main.dedServ)
			{
				return;
			}
			MapLoader.nameFuncs.Clear();
			MapLoader.entryToTile.Clear();
			MapLoader.entryToWall.Clear();
			Array.Resize<ushort>(ref MapHelper.tileLookup, 470);
			Array.Resize<ushort>(ref MapHelper.wallLookup, 231);
			Array.Resize<Color>(ref MapHelper.colorLookup, (int)MapHelper.modPosition);
			Lang._mapLegendCache.Resize((int)MapHelper.modPosition);
			MapLoader.initialized = false;
		}

		// Token: 0x060011DD RID: 4573 RVA: 0x0040BD08 File Offset: 0x00409F08
		internal static void ModMapOption(ref int mapType, int i, int j)
		{
			if (!MapLoader.entryToTile.ContainsKey((ushort)mapType))
			{
				if (MapLoader.entryToWall.ContainsKey((ushort)mapType))
				{
					ModWall wall = WallLoader.GetWall((int)MapLoader.entryToWall[(ushort)mapType]);
					ushort mapOption = wall.GetMapOption(i, j);
					if (mapOption < 0 || (int)mapOption >= MapLoader.modWallOptions(wall.Type))
					{
						throw new ArgumentOutOfRangeException("Bad map option for wall " + wall.Name + " from mod " + wall.mod.Name);
					}
					mapType += (int)mapOption;
				}
				return;
			}
			ModTile tile = TileLoader.GetTile((int)MapLoader.entryToTile[(ushort)mapType]);
			ushort mapOption2 = tile.GetMapOption(i, j);
			if (mapOption2 < 0 || (int)mapOption2 >= MapLoader.modTileOptions(tile.Type))
			{
				throw new ArgumentOutOfRangeException("Bad map option for tile " + tile.Name + " from mod " + tile.mod.Name);
			}
			mapType += (int)mapOption2;
		}

		// Token: 0x040013F5 RID: 5109
		internal static bool initialized = false;

		// Token: 0x040013F6 RID: 5110
		internal static readonly IDictionary<ushort, IList<MapEntry>> tileEntries = new Dictionary<ushort, IList<MapEntry>>();

		// Token: 0x040013F7 RID: 5111
		internal static readonly IDictionary<ushort, IList<MapEntry>> wallEntries = new Dictionary<ushort, IList<MapEntry>>();

		// Token: 0x040013F8 RID: 5112
		internal static readonly IDictionary<ushort, Func<string, int, int, string>> nameFuncs = new Dictionary<ushort, Func<string, int, int, string>>();

		// Token: 0x040013F9 RID: 5113
		internal static readonly IDictionary<ushort, ushort> entryToTile = new Dictionary<ushort, ushort>();

		// Token: 0x040013FA RID: 5114
		internal static readonly IDictionary<ushort, ushort> entryToWall = new Dictionary<ushort, ushort>();
	}
}
